package com.hospital.workstation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.hospital.common.core.domain.R;
import com.hospital.common.utils.StringUtils;
import com.hospital.common.core.page.TableDataInfo;
import com.hospital.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hospital.workstation.domain.bo.HisExtendOneInfoBo;
import com.hospital.workstation.domain.vo.HisExtendOneInfoVo;
import com.hospital.workstation.domain.HisExtendOneInfo;
import com.hospital.workstation.mapper.HisExtendOneInfoMapper;
import com.hospital.workstation.service.IHisExtendOneInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * HIS续一信息Service业务层处理
 *
 * @author whb
 * @date 2024-05-28
 */
@RequiredArgsConstructor
@Service
public class HisExtendOneInfoServiceImpl implements IHisExtendOneInfoService {

    private final HisExtendOneInfoMapper baseMapper;

    /**
     * 查询HIS续一信息
     */
    @Override
    public HisExtendOneInfoVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询HIS续一信息列表
     */
    @Override
    public TableDataInfo<HisExtendOneInfoVo> queryPageList(HisExtendOneInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HisExtendOneInfo> lqw = buildQueryWrapper(bo);
        Page<HisExtendOneInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询HIS续一信息列表
     */
    @Override
    public List<HisExtendOneInfoVo> queryList(HisExtendOneInfoBo bo) {
        LambdaQueryWrapper<HisExtendOneInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HisExtendOneInfo> buildQueryWrapper(HisExtendOneInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HisExtendOneInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMedicalRecordNo() != null, HisExtendOneInfo::getMedicalRecordNo, bo.getMedicalRecordNo());
        lqw.eq(bo.getBeginTime() != null, HisExtendOneInfo::getBeginTime, bo.getBeginTime());
        lqw.eq(StringUtils.isNotBlank(bo.getMenstruumType()), HisExtendOneInfo::getMenstruumType, bo.getMenstruumType());
        lqw.eq(StringUtils.isNotBlank(bo.getMenstruumTypeOther()), HisExtendOneInfo::getMenstruumTypeOther, bo.getMenstruumTypeOther());
        lqw.eq(StringUtils.isNotBlank(bo.getSingleMenstruumAmount()), HisExtendOneInfo::getSingleMenstruumAmount, bo.getSingleMenstruumAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getSingleAmount()), HisExtendOneInfo::getSingleAmount, bo.getSingleAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getGiveIllnessPathway()), HisExtendOneInfo::getGiveIllnessPathway, bo.getGiveIllnessPathway());
        lqw.eq(StringUtils.isNotBlank(bo.getGiveIllnessPathwayOther()), HisExtendOneInfo::getGiveIllnessPathwayOther, bo.getGiveIllnessPathwayOther());
        lqw.eq(StringUtils.isNotBlank(bo.getFrequency()), HisExtendOneInfo::getFrequency, bo.getFrequency());
        return lqw;
    }

    /**
     * 新增HIS续一信息
     */
    @Override
    public Boolean insertByBo(HisExtendOneInfoBo bo) {
        HisExtendOneInfo add = BeanUtil.toBean(bo, HisExtendOneInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改HIS续一信息
     */
    @Override
    public Boolean updateByBo(HisExtendOneInfoBo bo) {
        HisExtendOneInfo update = BeanUtil.toBean(bo, HisExtendOneInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HisExtendOneInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除HIS续一信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据药历号查询续一信息
     *
     * @param medicalRecordNo 药历号
     * @return
     */
    @Override
    public R<List<HisExtendOneInfoVo>> getHisExtendOneInfoList(Integer medicalRecordNo) {
        return R.ok(baseMapper.selectVoList(new LambdaQueryWrapper<HisExtendOneInfo>()
            .eq(HisExtendOneInfo::getMedicalRecordNo, medicalRecordNo)));
    }
}
